<%--
  Created by IntelliJ IDEA.
  User: 20426
  Date: 2020/7/20
  Time: 11:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员登录页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/back/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/back/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/back/layuiadmin/style/login.css" media="all">
</head>
<body>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>管理员登录</h2>
            <p>管理员后台登录管理系统</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <form class="form form-horizontal" id="loginInfo"  method="post">
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="userName"></label>
                    <input type="text" name="userName" id="userName" lay-verify="required" placeholder="管理员账号名" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="password"></label>
                    <input type="password" name="password" id="password" lay-verify="required" placeholder="密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="codeinput"></label>
                            <input type="text" name="code" id="codeinput" lay-verify="required" placeholder="图形验证码" onblur="if(this.value==''){this.value='验证码:'}" onclick="if(this.value=='验证码:'){this.value='';}" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <img src="code.jsp" id="code" style="width:170px;">
                                <a href="javascript:void(0);" onclick="changeCode()" >看不清,请点击</a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-manager"></label>
                    <input type="radio" name="managerType" id="common" checked="checked" value="普通管理员">普通管理员
                    <input type="radio" name="managerType" id="super"  value="超级管理员">超级管理员
                </div>

                <div class="layui-form-item" style="margin-bottom: 20px;">
                    <input type="checkbox" id="remember" name="remember" value="remember" lay-skin="primary" title="记住密码">
                </div>

                <div class="layui-form-item">
                    <input type="button" name="submitLogin" id="submitLogin" value="登录" style="width: 335px"  class="layui-btn  layui-btn-lg"/>
                </div>
            </form>
        </div>
    </div>

</div>

<script src="${pageContext.request.contextPath}/back/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/back/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index',function(){
        var $ = layui.$;
        //加载则判断本地缓存中是否有账号密码
        var storage=window.localStorage;
        if(storage.length>0){
            //存在则复选框选中，默认填充第一个账号
            $("#remember").prop("checked",true);
            var key=storage.key(0);
            $("#userName").val(key);//账号
            $("#password").val(localStorage.getItem(key));//密码
        }else{
            //不存在则复选框不选中
            $("#remember").prop("checked",false);
        }

        /*用户名改变时从localStorage中得到密码*/
        $(document).on("blur","#userName",function(){
            var loginUname = $(this).val();
            var loginPwd = localStorage.getItem(loginUname);
            if(loginPwd != null){
                //密码框赋值
                $("#password").val(loginPwd);
                $("#remember").prop("checked",true);
            }else{
                //localStorage中不存在则空串
                $("#password").val("");
                $("#remember").prop("checked",false);
            }
        });
        $("#submitLogin").click(function(){
            var codeinput =  $("#codeinput").val().toLowerCase();
            var managerInfo={
                adminName:$("#userName").val(),
                adminPwd:$("#password").val(),
                code:codeinput
            }
            // 获取复选框状态
            var checkbox =  $("#remember").prop("checked");
            // 获取管理员账号名
            var adminName = $("#userName").val();
            // 获取密码
            var adminPwd = $("#password").val();
            $.ajax({
                async:true,
                url: '${pageContext.request.contextPath}/back/login',
                type: 'POST',
                data: managerInfo,
                dataType:"json",
                success: function(result,status,xhr){
                    if(result=="codeerror"){
                        alert("验证码输入错误");
                    }
                    if(result == true){ // 登录成功
                        // 判断是否有记住密码
                        if(checkbox){
                            // 1.将用户信息存储在本地缓存中localStorage
                            window.localStorage.setItem(adminName,adminPwd);
                        }else{
                            // 1.将用户信息从本地缓存中localStorage清除
                            window.localStorage.removeItem(adminName);
                        }
                        if($('input[name=managerType]:checked').val()=="普通管理员"){
                            location.href = "${pageContext.request.contextPath}/back/views/commonManagerIndex.jsp";
                        }else if($('input[name=managerType]:checked').val()=="超级管理员"){
                            location.href = "${pageContext.request.contextPath}/back/views/superManagerIndex.jsp";
                        }else{
                            alert("请选择管理员类型");
                        }
                        // 2.页面跳转到首页index.jsp

                    }else if ((result == false)) { // 登录失败
                        // 提示
                        alert("用户名或密码错误!");
                        // 清空输入的密码
                        $("#password").val("");
                    }
                },
                error:function(result,status,xhr) {
                    alert(result);
                    alert("管理员登陆失败");
                },

            });
        });
    });
    function changeCode() {
        document.getElementById("code").src = "${pageContext.request.contextPath}/back/views/code.jsp?="+new Date().getTime();
    }
</script>
</body>
</html>